package org.alex.samples.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alex.samples.filter.PagerModel;
import org.alex.samples.model.CommonMessageResultVO;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.model.ExamTestTypeVO;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/teacher")
public class TestController {
	
	@Autowired
	private ITestService testService;
	
/*	@RequestMapping(value = "/tests")
	public String test(Model model){
		List<ExamTestVO> list = testService.findAllTest();
		model.addAttribute("testList",list);
		return "test/testList";
		
	}*/
	
	@RequestMapping(value = "/tests")
	public String testPage(Model model){
		PagerModel<ExamTestVO> page = testService.findAllByPage();
		model.addAttribute("page",page);
		return "test/testList";
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/getSubjectAndTypes",method = RequestMethod.POST)
	public Map<String,Object> getSubjectAndTypes(){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		List<ExamSubjectVO> subjectList = testService.getSubectList();
		List<ExamTestTypeVO> testTypeList = testService.getTestTypeList();
		resultMap.put("subjectList", subjectList);
		resultMap.put("testTypeList", testTypeList);
		return resultMap;
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/getTestById",method = RequestMethod.POST)
	public ExamTestVO getTestById(Integer id){
		ExamTestVO resultVO = testService.getTestVOById(id);
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveTest",method = RequestMethod.POST)
	public CommonMessageResultVO saveTest(ExamTestVO examTestVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String,String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
		}
		try{
			testService.createTest(examTestVO);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateTest",method = RequestMethod.POST)
	public CommonMessageResultVO updateTest(ExamTestVO examTestVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String,String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
		}
		try{
			testService.editTest(examTestVO);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/deleteTest",method = RequestMethod.POST)
	public CommonMessageResultVO deleteTest(Integer id){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String,String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		try {
			testService.deleteTest(id);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
}
